package com.token.iot.bratterycharging.gateway;

import com.outstanding.framework.base.SpringContextHolder;
import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.dao.InvalidOperDAO;


/**
 * 上行指令响应器的工厂类
 * @author chenzhixuan
 *
 */
public class OperFactory {
	
	//上行指令生成器
	static public Oper createOper(String code, String imei) throws PendingException {
		Oper  oper = null;
		//进行码转换
		String newCode = OperExchanger.getSendCode(code);
		if(newCode == null) {
			newCode = code;
		}
		try {
			oper= (Oper) SpringContextHolder.getBean(newCode+"Oper");
		} catch (Exception e) {
			//e.printStackTrace();
		}
		if(oper == null) {
			try {
				oper = (Oper) SpringContextHolder.getBean(newCode+"DuplexProcessor");
			} catch (Exception e) {
				//e.printStackTrace();
			}
		}
		if (oper == null) {
			InvalidOper inOper = new InvalidOper();
			inOper.setImei(imei);
			inOper.setContext("上行指令");
			inOper.setOper(newCode);
			InvalidOperDAO dao = (InvalidOperDAO) SpringContextHolder.getBean(InvalidOperDAO.class);
			dao.insert(inOper);
			throw new PendingException(ResCode.OPERNOTFIND);
		}
		oper.setCode(code);
		return oper;

	}
}
